<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

class Member extends Base {
    protected $table = 'think_user';
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    public function getWeekData($field = []){
        $count = $this
            ->whereTime('create_time', 'week')
            ->where('is_auth','eq', 0)
            ->count('*');
        $day = date('w');
        $_list = [];
        $d = date('Y-m-d',time());
        for($i = $day;$i > 0;$i--){
            $j = $i -1;
            $time = date('Y-m-d',strtotime("{$d} -{$j} day"));
            $expectedData = $this
                ->whereBetweenTime('create_time',$time)
                ->where('is_auth','eq', 0)
                ->count('*');
            $actualData = $this
                ->whereBetweenTime('create_time',$time)
                ->where('is_auth','neq', 0)
                ->count('*');
            $_list[] = [
                'expectedData'=>$expectedData,
                'actualData'=>$actualData
            ];
        }
        return [
            'total'=>$count,
            'list'=>$_list
        ];
    }

    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->with('admin')
            ->find($id);
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function admin(){
        return $this->hasOne('Admin','id','aid')
            ->bind([
                'username',
                'nickname'
            ]);
    }
}